<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象,内置对象案例</title>
    <script>
        //字面量创建对象
/*         var obj = {
            name: '哈哈',
            num: 100,
            sayHi: function(){
                console.log('hi');
            }
        }
        obj.sayHi();
        console.log(obj.name);
        console.log(obj.num); */

        //new Object创建对象
/*         var obj = new Object();
        obj.name = '一百';
        obj.num = 100;
        for (var k in obj) console.log(obj[k]); */

        //构造函数创建对象
/*         function Character(uname, type, blood, attack) {
            this.name = uname;
            this.type = type;
            this.blood = blood;
            this.attack = attack;
        }
        var lianpo = new Character('廉颇', '力量型', '500', '近战');
        var houyi = new Character('后羿', '射手型', '100', '远程');
        for (var k in lianpo) console.log(lianpo[k]);
        for (var k in houyi) console.log(houyi[k]); */

        //Math对象实例 - 猜数字游戏
/*         function getRandom(min, max) {
            return Math.floor(Math.random()*(max-min+1)) + min;
        }
        var ans = getRandom(1, 2000);
        while(true) {
            var num = prompt('请在1-2000中输入一个数字');
            if (num > ans)
                alert('大了,继续猜');
            else if (num < ans)
                alert('小了,继续猜');
            else if (num = ans) {
                alert('猜对了!');
                break;
            }
        } */

        //Date对象实例 - 格式化当前日期
/*         var date = new Date();

        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var dates = date.getDate();
        var day = date.getDay();
        var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        
        console.log(date);
        console.log('现在是' + year + '年' + month + '月' + dates + '日 ' + arr[day]); */
        
        //Date对象实例 - 格式化时分秒(调用函数)
/*         function getTime() {
            var date = new Date();
            var h = date.getHours();
            var min = date.getMinutes();
            var s = date.getSeconds();
            
            h = zero(h);
            min = zero(min);
            s = zero(s);
            
            return h + ':' + min + ':' + s;
        }
        function zero(x) {
            return x < 10 ? '0' + x : x;
        }
        console.log(getTime()); */

        //Date对象实例 - 倒计时
/*         function zero(x) {
            return x < 10 ? '0' + x : x;
        }
        function countdown(time) {
            var nowTime = +new Date();
            var inputTime = +new Date(time);

            //毫秒转换为秒
            times = (inputTime - nowTime) / 1000;

            d = zero(parseInt(times / 60 / 60 / 24));
            h = zero(parseInt(times / 60 / 60 % 24));
            m = zero(parseInt(times / 60 % 60));
            s = zero(parseInt(times % 60));

            return d + '天' + h + '小时' + m + '分' + s + '秒';
        }
        console.log(countdown('2021-6-6 00:00:00')); */

        //数组对象 - 去重
/*         var arr = [2, 0, 6, 1, 77, 0, 52, 0, 25, 7];
        var newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (newArr.indexOf(arr[i]) == -1)
                newArr.push(arr[i]);
        }
        console.log(newArr); */

        //字符串对象 - 返回字符位置
/*         var str = "apple";
        var time = 0;
        var index = str.indexOf('p');
        console.log('索引号为:');
        while (index !== -1) {
            console.log(index);
            index = str.indexOf('p', index +1);
            time++;
        }
        console.log('次数为：' + time); */

        //字符串对象 - 统计出现次数最多的字符
        var str = "apple";
        var o = {};
        var time = 0;
        var max = '';
        for (var i = 0; i < str.length; i++) {
            var chars = str.charAt(i);
            if (o[chars])
                o[chars]++;
            else
                o[chars] = 1;
        }
        console.log(o);
        for (var k in o) {
            if (o[k] > time) {
                time = o[k];
                max = k;
            }
        }
        console.log('出现最多的为：' + max + "，出现" + time + '次');

    </script>
</head>
<body>
    
</body>
</html>